package com.mshen.service;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import com.mashen.client.WeiXinClient;
import com.mashen.client.model.UserInfo;
import com.mashen.dao.RecommandDao;
import com.mashen.dao.model.BerecommandEntity;
import com.mashen.dao.model.RecommandEntity;
import com.mashen.util.ConfigUtil;
import com.mashen.util.Constant;
import com.mashen.util.MessageUtil;
import com.mshen.service.model.TextResponseMessage;
import com.mshen.service.model.WeixinRequest;
public class WeixinService {
	private Executor executor=Executors.newFixedThreadPool(20);
	private RecommandDao recomdao=new RecommandDao();
	private int maxrecommand=Integer.parseInt(ConfigUtil.getValue("maxrecommand"));
		public String processMsg(WeixinRequest request) throws Throwable{
		System.out.println("getEventKey:"+request.getEventKey());
		if(Constant.MSG_TYPE_EVENT.equals(
		request.getMsgType())){
		if(Constant.MSG_EVENT_CLICK.equals(request.getEvent())){
		if(Constant.CREATE_PIC.equals(request.getEventKey())){
					String result = getPic(request);
					if(result==null){//数据库中没有记录，则启动线程生成
					CreatePicTask task=new CreatePicTask(request,recomdao);
					executor.execute(task);//此方法不会阻塞
					return getTextMsg(request,"正在生产海报，请稍候再点击获取！");
					    }else{
						return result;
					}
				}
			}
			/**
			 * 捕获订阅事件，预先生成图片
			 */
			if(Constant.MSG_EVENT_SUBSCRIBE.equals(
					request.getEvent()))
			{
				CreatePicTask task=new CreatePicTask(request,recomdao);
				executor.execute(task);
				String eventkey=request.getEventKey();
				System.out.println("eventkey:"+eventkey);
				if(eventkey!=null&&!"".equals(eventkey)){
					if(eventkey.startsWith(Constant
							.MSG_EVENT_SUB_QRSCENE)){
						//扫带参数二维码订阅事件
						//qrscene_123123
						int recomid=Integer.parseInt(eventkey
								.replace(Constant
								.MSG_EVENT_SUB_QRSCENE,""));
						//openid, nickname, recommandid
						//openid目前是被推荐人的
						RecommandEntity recom=recomdao.get(recomid);
						String recomopenid=recom.getOpenid();//获取推荐人ID
						String berecomopenid=request.getFromUserName();//被推荐人的ID
						WeiXinClient.sendkfMessage(recomopenid, "你成功推荐了一个人");
						WeiXinClient.sendkfMessage(berecomopenid, "欢迎关注搜融俱乐部");
						}
						
					}
				}
			}
		
		
		if(Constant.MSG_TYPE_TEXT.equals(
				request.getMsgType())){
			 
		}
		return getDefaultMsg(request);
	}
	public String getDefaultMsg(WeixinRequest request){
		TextResponseMessage textmsg=new TextResponseMessage();
		textmsg.setToUserName(request.getFromUserName());
		textmsg.setFromUserName(request.getToUserName());
		textmsg.setMsgType(Constant.MSG_TYPE_TEXT);
		textmsg.setCreateTime(System.currentTimeMillis());
		textmsg.setContent("欢迎加入搜融俱乐部！");
		return MessageUtil.toXML(textmsg);
	}
	public String getTextMsg(WeixinRequest request,String content){
		TextResponseMessage textmsg=new TextResponseMessage();
		textmsg.setToUserName(request.getFromUserName());
		textmsg.setFromUserName(request.getToUserName());
		textmsg.setMsgType(Constant.MSG_TYPE_TEXT);
		textmsg.setCreateTime(System.currentTimeMillis());
		textmsg.setContent("");
		return MessageUtil.toXML(textmsg);
	}
	public String getPic(WeixinRequest request) throws Throwable{
		RecommandEntity entity=recomdao.getByOepnid(request.getFromUserName());
		if(entity!=null){
		return entity.getPicresult();
		}else{
		return null;
		}
	}
	
}
